package com.siyao.redvip.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.siyao.redvip.R;
import com.siyao.redvip.RedApplication;
import com.siyao.redvip.bean.FriendCircle;
import com.siyao.redvip.bean.HDxxBean;
import com.siyao.redvip.components.view.MeasuredGridView;
import com.siyao.redvip.components.view.MoreImageView;
import com.siyao.redvip.image.ImageLoaderOptions;
import com.siyao.redvip.ui.activity.FriendDetailsActivity;
import com.siyao.redvip.utils.ToastUtils;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FriendAdapter extends BaseAdapter{
	
	private List<FriendCircle>  friendCircles;
	private LayoutInflater mInflater;// 加载list_item
	private Context context;
	private ButtonCallBack buttonCallBack;
	public FriendAdapter(Context context) {
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	public void setFriendCircles(List<FriendCircle> friendCircles) {
		this.friendCircles = friendCircles;
	}

	@Override
	public int getCount() {
		return friendCircles.isEmpty() ? 0 : friendCircles.size();
	}

	@Override
	public Object getItem(int position) {
		return position < 0 ? 0 : position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView==null){
			viewHolder = new ViewHolder();
			convertView=mInflater.inflate(R.layout.item_friend_citcle, null);
			viewHolder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
			viewHolder.more_image =  (MoreImageView) convertView.findViewById(R.id.more_image);
			viewHolder.iv_user_icon =  (ImageView) convertView.findViewById(R.id.iv_user_icon);
			viewHolder.say_image =  (ImageView) convertView.findViewById(R.id.say_image);
			viewHolder.tv_time =  (TextView) convertView.findViewById(R.id.tv_time);
			viewHolder.list_view_replay=(ListView) convertView.findViewById(R.id.list_view_replay);
			convertView.setTag(viewHolder);
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
		
		final FriendCircle friendCircle=friendCircles.get(position);
		viewHolder.say_image.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int vId=v.getId();
				switch (vId) {
				case R.id.say_image:
					Intent intent = new Intent();
					intent.setClass(context, FriendDetailsActivity.class);
					Bundle mBundle = new Bundle();
					mBundle.putSerializable("friendCircle", friendCircle);
					intent.putExtras(mBundle);
					context.startActivity(intent);
					break;
				default:
					break;
				}
			}
		});
		
		ImageLoader.getInstance().displayImage(friendCircle.getHeadPortrait(), viewHolder.iv_user_icon,ImageLoaderOptions.getHeaderOptions());
		viewHolder.tv_username.setText(friendCircle.getName());
		viewHolder.tv_time.setText(friendCircle.getTime());
		if(!TextUtils.isEmpty(friendCircle.getImages())){
			String [] images= friendCircle.getImages().split(",");
			List<String> paths=new ArrayList<String>();
			for (int i = 0; i < images.length; i++) {
				paths.add(images[i]);
			}
			viewHolder.more_image.setPaths(paths);
		}
		
		if(!friendCircle.getCommentList().isEmpty()){
			viewHolder.list_view_replay.setVisibility(View.VISIBLE);
			CommentAdapter commentAdapter = new CommentAdapter(this.context);
			commentAdapter.setComments(friendCircle.getCommentList());
			viewHolder.list_view_replay.setAdapter(commentAdapter);
		}else{
			viewHolder.list_view_replay.setVisibility(View.GONE);
		}
		
		return convertView;
	}
	
	private static class ViewHolder {
		public TextView tv_username;
		public TextView tv_time;
		public MoreImageView  more_image;
		public ImageView iv_user_icon;
		public ImageView say_image;
		public ListView list_view_replay;
	}
	
	public void setDataChanged(List<FriendCircle> mDatas) {
        if (mDatas != null && !mDatas.isEmpty()) {
            this.friendCircles = mDatas;
            this.notifyDataSetChanged();
        }
    }

    public void clear() {
        if (friendCircles != null && !friendCircles.isEmpty()) {
            this.friendCircles.clear();
        }
        this.notifyDataSetChanged();
    }
    
    public  interface ButtonCallBack{
    	public void callBack();
    }

	public void setCallBack(ButtonCallBack buttonCallBack) {
		this.buttonCallBack=buttonCallBack;
	}
}
